/*
 * Created by JFormDesigner on Mon Jun 26 09:06:52 CST 2023
 */

package org.scct.chenhui.frames;

import org.scct.chenhui.dao.UserDao;
import org.scct.chenhui.model.UserBean;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

/**
 * @author Administrator
 */
public class FrmRegister extends JFrame {
    public FrmRegister() {
        initComponents();
        initData();
    }

    public static void main(String[] args) {
        new FrmRegister().setVisible(true);
    }
    private void initData() {
        btnRegister.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.获取用户输入的信息
                String name = txtName.getText();
                String pass = txtPass.getText();
                String pass2 = txtPass2.getText();
                String phone = txtPhone.getText();
                String sex=radMale.isSelected()?"男":"女";

                //2 验证格式
                if(!name.matches("^\\w{3,10}$"))
                {
                    JOptionPane.showMessageDialog(null,"用户名格式错误");
                    return;
                }
                if(!pass.equals(pass2))
                {
                    JOptionPane.showMessageDialog(null,"两次输入的密码不一致");
                    return;
                }
                if(!pass.matches("^\\w{6,12}$"))
                {
                    JOptionPane.showMessageDialog(null,"密码格式错误");
                    return;
                }
                if(!phone.matches("^1[3-9]\\d{9}$"))
                {
                    JOptionPane.showMessageDialog(null,"手机号码格式错误");
                    return;
                }
                //3.判断用户名和电话号码是否重复
                UserDao userDao = new UserDao();
                if(userDao.isDoubleUserName(name))
                {
                    JOptionPane.showMessageDialog(null,"用户名已存在，请重新输入");
                    return;
                }
                if(userDao.isDoublePhone(phone))
                {
                    JOptionPane.showMessageDialog(null,"电话号码已存在，请重新输入");
                    return;
                }

                //4注册，添加用户
                UserBean userBean = new UserBean();
                userBean.setUsername(name);
                userBean.setPassword(pass);
                userBean.setRole(1);
                userBean.setSex(sex);
                userBean.setPhone(phone);
                if(userDao.AddUser(userBean))
                {
                    JOptionPane.showMessageDialog(null,"注册成功，请进行用户登陆");
                    //5.跳转到登陆界面
                    dispose();
                    FrmLogin frmLogin = new FrmLogin();
                    frmLogin.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null,"注册失败，请联系管理员");
                }

            }
        });
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - brightch
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        btnRegister = new JButton();
        btnCancel = new JButton();
        txtName = new JTextField();
        txtPass = new JPasswordField();
        txtPass2 = new JPasswordField();
        txtPhone = new JTextField();
        radMale = new JRadioButton();
        radFemale = new JRadioButton();

        //======== this ========
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u6ce8\u518c");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 18));
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(200, 30), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(50, 80), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(55, 120), label3.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u786e\u8ba4\u5bc6\u7801");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(55, 155), label4.getPreferredSize()));

        //---- label5 ----
        label5.setText("\u624b\u673a\u53f7\u7801");
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(55, 190), label5.getPreferredSize()));

        //---- label6 ----
        label6.setText("\u6027\u522b");
        contentPane.add(label6);
        label6.setBounds(new Rectangle(new Point(55, 225), label6.getPreferredSize()));

        //---- btnRegister ----
        btnRegister.setText("\u6ce8\u518c");
        contentPane.add(btnRegister);
        btnRegister.setBounds(new Rectangle(new Point(105, 275), btnRegister.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(255, 275), btnCancel.getPreferredSize()));
        contentPane.add(txtName);
        txtName.setBounds(125, 75, 200, txtName.getPreferredSize().height);
        contentPane.add(txtPass);
        txtPass.setBounds(125, 110, 200, txtPass.getPreferredSize().height);
        contentPane.add(txtPass2);
        txtPass2.setBounds(125, 145, 200, txtPass2.getPreferredSize().height);
        contentPane.add(txtPhone);
        txtPhone.setBounds(125, 180, 200, txtPhone.getPreferredSize().height);

        //---- radMale ----
        radMale.setText("\u7537");
        radMale.setSelected(true);
        contentPane.add(radMale);
        radMale.setBounds(new Rectangle(new Point(130, 230), radMale.getPreferredSize()));

        //---- radFemale ----
        radFemale.setText("\u5973");
        contentPane.add(radFemale);
        radFemale.setBounds(new Rectangle(new Point(200, 230), radFemale.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(445, 365);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - brightch
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JButton btnRegister;
    private JButton btnCancel;
    private JTextField txtName;
    private JPasswordField txtPass;
    private JPasswordField txtPass2;
    private JTextField txtPhone;
    private JRadioButton radMale;
    private JRadioButton radFemale;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
